from appium.webdriver.webdriver import WebDriver
from selenium.common.exceptions import NoSuchElementException
import logging

class BasePage:
    def __init__(self , driver: WebDriver):
        self.driver = driver

    def find(self,by,locator):
        return self.driver.find_element(by,locator)

    def find_click(self,by,locator):
        '''
        查找到元素并点击
        :param by:
        :param locator:
        :return:
        '''
        self.find(by,locator).click()

    def find_sendkeys(self,by,locator,text):
        '''
        找到元素后输入
        :param by:
        :param locator:
        :param text:
        :return:
        '''
        self.find(by,locator).send_keys(text)

    def swipe_find(self, text, num=3):
        # 滑动查找
        for i in range(num):
            try:
                return self.driver.find_element(MobileBy.XPATH, f'//*[@text = "{text}"]')
            except:
                size = self.driver.get_window_size()
                width = size.get("width")
                height = size.get("height")
                start_x = width / 2
                start_y = height * 0.8
                end_x = start_x
                end_y = height * 3
                duration = 2000
                self.driver.swipe(start_x, start_y, end_x, end_y, duration)
            if i == num - 1:
                raise NoSuchElementException(f"找了{num}次，未找到")

    def get_attribut(self,by,locator,text):
        self.find(by,locator).get_attribute(text)